<template>
  <van-popup v-bind="$attrs" position="right" style="width:95%;height:100%" :close-on-click-overlay="false">
    <div class="content ">
      <div class=" ">
        <div class="title">
          <span>选择成员</span>
          <!-- <van-icon name="cross" @click="close" /> -->
        </div>
        <van-search v-model="query.userName" placeholder="请输入搜索关键词" clearable @input="onSearch" />
        <van-list v-if="list.length" v-model="loading" :finished="finished" finished-text="已经到底啦" @load="onLoad">
          <div
            v-for="(item, i) in list"
            :key="i"
            class="cell-item"
            :class="selectList.some((e) => e.userId == item.userId) ? 'cell-item-active' : ''"
            @click="cellClick(item)"
          >
            <div class="cell-item-left">
              <!-- <img class="avatar" :src="item.avatar" /> -->
              <div class="user-name">
                <span>{{ item.userName }}</span>
              </div>
            </div>
            <van-icon
              v-show="selectList.some((e) => e.userId == item.userId)"
              name="success"
              class="check-outlined-icon"
            />
          </div>
        </van-list>
        <Empty v-else />
      </div>

      <div class="footer flex" style="margin: 20px;">
        <van-button class="" native-type="button" round block plain @click="cancel">取消</van-button>
        <van-button class="ml10" native-type="button" round block plain @click="reset">重置</van-button>
        <van-button class="ml10" type="info" native-type="submit" round block @click="submit">提交</van-button>
      </div>
    </div>
  </van-popup>
</template>

<script>
import { getStaffAll } from '@/api/common'

export default {
  components: {},
  data() {
    return {
      loading: false,
      finished: false,
      query: {
        pageSize: 10,
        pageNum: 1,
        userName: '',
      },
      total: 0,
      list: [],
      selectList: [],
    }
  },
  created() {
    this.getUserList()
  },
  methods: {
    onSearch() {
      this.query.pageNum = 1
      this.list = []
      this.total = 0
      this.loading = true
      this.finished = false
      this.getUserList()
    },
    onLoad() {
      if (this.total > this.list.length) {
        this.query.pageNum++
        this.getUserList()
      } else {
        this.finished = true
      }
    },
    getUserList() {
      this.loading = true
      getStaffAll(this.query).then(({ data, total }) => {
        this.loading = false
        this.total = +total
        // data.forEach((el, i) => {
        //   el.avatar || (el.avatar = customerAatars[Math.floor(Math.random() * 10)])
        // })
        this.list.push(...data)
      })
    },
    cancel() {
      this.$emit('input', false)
    },
    reset() {
      this.selectList = []
    },
    submit() {
      this.$emit('change', JSON.parse(JSON.stringify(this.selectList)))
      this.reset()
      this.cancel()
    },
    cellClick(item) {
      let index = this.selectList.findIndex((e) => e.userId == item.userId)
      index > -1 ? this.selectList.splice(index, 1) : this.selectList.push(item)
    },
  },
}
</script>

<style lang="less" scoped>
.content {
  height: 100%;
  display: flex;
  flex-direction: column;
}
.content {
  .title {
    display: flex;
    justify-content: space-between;
    padding: 20px 15px 10px;
    font-size: 16px;
  }
}

// .search {
//   display: flex;
//   margin-left: 25px;
//   margin: 15px 25px;
//   .search-icon {
//     font-size: 17px;
//     margin-top: 4px;
//   }
// }
.van-list {
  height: calc(100vh - 220px);
  overflow: auto;
  padding: 15px 15px 0;
  // margin-top: 15px;
  background: #f2f2f2;
  overflow: auto;
}
// .van-search__content {
//   background-color: #fff;
//   font-size: 12px;
//   font-family: PingFangHK-Regular, PingFangHK;
//   font-weight: 400;
//   color: #86909c;
// }
// .van-search {
//   padding: 0;
// }
.cell-item {
  display: flex;
  width: 100%;
  align-items: center;
  justify-content: space-between;
  padding: 10px 15px;
  background: #fff;
  border-radius: 8px;
  & + & {
    border-top: 1px solid #f2f4f5;
    margin-top: 10px;
  }
  .cell-item-left {
    display: flex;
    align-items: center;
    .avatar {
      width: 50px;
      height: 50px;
      border-radius: 50%;
    }
    .user-name {
      margin-left: 20px;
      width: 180px;
      font-size: 17px;
      font-family: PingFangSC-Regular, PingFang SC;
      font-weight: 400;
      color: #1d2129;
      overflow: hidden;
      white-space: nowrap;
      text-overflow: ellipsis;
      line-height: 44px;
    }
  }
  .check-outlined-icon {
    // width: 22px;
    // height: 22px;
    font-size: 22px;
    color: #3f7ef2;
  }
}
.cell-item-active {
  background: rgba(63, 126, 242, 0.1);
}
</style>
